package com.example.sls.controller.teacher;

import com.example.sls.pojo.Class._Class;
import com.example.sls.pojo.student.Student;
import com.example.sls.pojo.teacher.Teacher;
import com.example.sls.service.Class.ClassService;
import com.example.sls.service.student.StudentService;
import com.example.sls.service.teacher.TeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    ClassService classService;

    @Autowired
    StudentService studentService;


    @GetMapping("/getInfoByTId")
    public Teacher getTeacherInfo(@RequestParam Map<String, String> params){
//        System.out.println("========");
//        System.out.println("params: " + params);
//        System.out.println("========");
        Integer TId = Integer.valueOf(params.get("TId"));
        return teacherService.getTeacherByTId(TId);
    }


    //分页获取班级信息
    @GetMapping("/classesInfo/{TId}/{pageNum}/{pageSize}")
    public PageInfo<_Class>getPageInfoByTId(@PathVariable("TId") Integer TId,@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){

        return classService.getPageInfoByTId(TId, pageNum, pageSize);
    }


    //通过classId分页获取学生信息
    @GetMapping("/studentsInfo/{CId}/{pageNum}/{pageSize}")
    public PageInfo<Student>getPageInfoByCId(@PathVariable("CId") Integer CId,@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return studentService.getPageInfoByCId(CId, pageNum,pageSize);
    }

    //分页获取未加入班级的学生的信息
    @GetMapping("/pageInfoWithoutClass/{pageNum}/{pageSize}")
    public PageInfo<Student> getPageInfoWithoutClass(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        return studentService.getPageInfoWithoutClass(pageNum,pageSize);
    }

    //更改学生的classId(同时需要更新classId为cId的班级的人数 和 studentId为studentId的学生的班级号)

    @PostMapping("/updateCIdBySId")
    public Map<String, String> updateCIdBySId(@RequestParam Map<String, String> params){

        Integer studentId = Integer.valueOf(params.get("studentId"));
        Integer cId = Integer.valueOf(params.get("cId"));

        System.out.println("========");
        System.out.println(studentId);
        System.out.println(cId);
        System.out.println("========");

        Map<String , String> res = new HashMap<>();

        if(studentService.updateCIdBySId(cId,studentId)){
            //修改完当前学生的cId之后,对应班级的人数增加
            Integer cnt = studentService.getCountByCId(cId);

            if(classService.setCountByCId(cId,cnt)){
                res.put("error_message", "success");
             }
            else{
                res.put("error_message", "修改出错");
             }
            }
        else{
            res.put("error_message", "修改出错");
        }
        return res;
    }


}
